package com.atguigu.eduservice.controller;


import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.atguigu.commonutils.R;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-01
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    //课程列表，带条件分页
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public R pageCourseCondition(@PathVariable int current,
                                 @PathVariable int limit,
                                 @RequestBody(required = false) CourseQuery courseQuery) {
        Page<EduCourse> eduCoursePage = courseService.pageCourseCondition(current, limit, courseQuery);
        long total = eduCoursePage.getTotal();//该页中记录数
        List<EduCourse> records = eduCoursePage.getRecords();//数据
        return R.ok().data("list", records).data("total", total);
    }

    //添加课程信息的方法
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        courseService.saveCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程id获取课程基本信息
    @ApiOperation("获取课程基本信息")
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@ApiParam(name = "courseId", value = "课程ID", required = true)
                           @PathVariable String courseId) {
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo", courseInfoVo);
    }

    //修改课程信息
    @PostMapping("/updateCourseInfo")
    private R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    @ApiOperation("根据课程ID查询课程确认信息")
    @GetMapping("getPublishInfo/{id}")
    public R updateCourseInfo(@ApiParam(name = "id", value = "课程ID", required = true)
                              @PathVariable String id) {
        CoursePublishVo coursePublishVo = courseService.getPublishInfo(id);
        return R.ok().message("课程确认信息获取成功").data("publishCourse", coursePublishVo);
    }

    //课程最终发布，修改课程状态为Normal
    @GetMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {
        Boolean b = courseService.publishCourse(id);
        return b ? R.ok().message("课程发布成功") : R.error().message("课程发布失败");
    }

    //根据课程id删除课程
    @ApiOperation("删除课程")
    @DeleteMapping("{courseId}")
    public R deleteCourse(@ApiParam(name="courseId",value = "课程ID",required = true)
                              @PathVariable String courseId) {
        courseService.deleteCourse(courseId);
        return R.ok();
    }
}

